package edu.bbu.license.client.android.controller.guest;

import android.view.View;
import edu.bbu.license.client.android.R;
import edu.bbu.license.client.android.controller.BaseController;
import edu.bbu.license.client.android.gui.guest.GuestView;
import edu.bbu.license.client.android.services.exceptions.IdentificationException;
import edu.bbu.license.client.android.services.exceptions.NetworkException;
import edu.bbu.license.client.android.services.user.worker.SimpleAutentificationService;

public class GuestController extends BaseController {
	
	private volatile GuestView guestView = null;
	private static GuestController controllerInstance = null;
	
	private GuestController() { }
	
	public static synchronized GuestController getInstance() {
		if(controllerInstance == null) {
			controllerInstance = new GuestController();
		}
		
		return controllerInstance;
	}
	
	public synchronized void atach(GuestView guestView) {
		if(guestView != null) {
			this.guestView = guestView;
			initializeView();
		}
	}
	
	private void initializeView() { 
		guestView.setHeaderLoginButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				guestView.setActivePage(GuestView.LOGIN_PAGE);
			}
		});
		
		guestView.setHeaderRegisterButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				guestView.setActivePage(GuestView.REGISTER_PAGE);
			}
		});
		
		guestView.setHeaderForgotPwdButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				guestView.setActivePage(GuestView.FORGORPWD_PAGE);
			}
		});
		
		guestView.setGuestLoginButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				executeInNewThread(new Runnable() {
					
					@Override
					public void run() {						
						guestView.resetLoginError();
						guestView.showLoginProgressIndicator();
								
						doFloodPreventiveSleep(3);
						
						String email = guestView.getLoginEmail();
						String password = guestView.getLoginPassword();
						
						if(email.isEmpty() || password.isEmpty()) {
							guestView.setLoginError(R.string.guest_activity_login_page_login_error);
							guestView.hideLoginProgressIndicator();
							
							return;
						}
						
						try {
							if(getAutentificationService().isValidAutentificationData(email, password)) {
								((SimpleAutentificationService)getAutentificationService()).setCurrentlyLoggedInUser(email);
								guestView.openFilesView();
								guestView.closeGuestView();
							} else {
								guestView.setLoginError(R.string.guest_activity_login_page_login_error);
							}
						} catch (NetworkException e) {
							guestView.setLoginError(R.string.app_network_error);
						}

						guestView.hideLoginProgressIndicator();
					}
				});
			}
		});
		
		guestView.setGuestRegisterButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {					
				executeInNewThread(new Runnable() {
					
					@Override
					public void run() {
						guestView.resetRegisterError();
						guestView.showRegisterProgressIndicator();
						
						doFloodPreventiveSleep(3);
						
						String email = guestView.getRegisterEmail();
						String confemail = guestView.getRegisterConfEmail();
						
						if(email.isEmpty() || confemail.isEmpty() || !email.contains("@") || !email.contains(".")) {
							guestView.setRegisterError(R.string.guest_activity_register_page_format_email_error);
							guestView.hideRegisterProgressIndicator();
							return;
						} else if(!email.equals(confemail)) {
							guestView.setRegisterError(R.string.guest_activity_register_page_diffrent_email_error);
							guestView.hideRegisterProgressIndicator();
							return;
						}
						
						try {
							getAutentificationService().registerNewUser(email);
							guestView.setRegisterError(R.string.guest_activity_register_page_register_successfully);
						} catch (IdentificationException e) {
							guestView.setRegisterError(R.string.guest_activity_register_page_existent_email_error);
						} catch (NetworkException e) {
							guestView.setRegisterError(R.string.app_network_error);
						} 

						guestView.hideRegisterProgressIndicator();
					}
				});
			}
		});
		
		guestView.setGuestForgotPwdButtonListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				executeInNewThread(new Runnable() {
					
					@Override
					public void run() {
						guestView.resetForgotPwdError();
						guestView.showForgotPwdProgressIndicator();
						
						doFloodPreventiveSleep(3);
						
						String email = guestView.getForgotPwdEmail();
						
						if(email.isEmpty() || !email.contains("@") || !email.contains(".")) {
							guestView.setForgotPwdError(R.string.guest_activity_forgotpwd_page_format_email_error);
							guestView.hideForgotPwdProgressIndicator();
							return;
						}
						
						try {
							getAutentificationService().generateNewPassword(email);
							guestView.setForgotPwdError(R.string.guest_activity_forgotpwd_page_send_successfull);
						} catch (IdentificationException e) {
							guestView.setForgotPwdError(R.string.guest_activity_forgotpwd_page_inexistent_email_error);
						} catch (NetworkException e) {
							guestView.setForgotPwdError(R.string.app_network_error);
						} 
						
						guestView.hideForgotPwdProgressIndicator();
					}
				});
			}
		});	
	}
}
